<?php
/**
 * 《我的供应商》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');

class SupplierMy extends Supplier {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}

	function find($controller, $request) {
		$this->pretreat($controller);
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'contact/supplier/my/find');
		return true;
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['contact_'] = $this->_num_contact($row['id']);
				$row['bank_'] = $this->_num_bank($row['id']);								

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);
				
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'contact/supplier/my/list');
		return true;
	}
	
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$company = $request['company'];
		$code = $request['code'];
		$name = addslashes($request['name']);
	
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);
	
		$fullname = addslashes($request['fullname']);
	
		$cert_type = $request['cert_type'];
		$cert_no = trim($request['cert_no']);
	
		$star = $request['star'];
		$category = addslashes($request['category']);
		$type = addslashes($request['type']);
	
		$province = $request['province'];
		$city = $request['city'];
		$district = $request['district'];
	
		$address = addslashes($request['address']);
		$geo = strtolower($request['geo']);
	
		$url = strtolower($request['url']);
		$share = addslashes($request['share']);
		$jj = addslashes($request['jj']);
	
		$content = addslashes($request['content']);
	
		$pay = addslashes($request['pay']);
		$currency = $request['currency'];
		$tax = $request['tax'];
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$status = $request['status'];
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', name = '%s', zjm = '%s', fullname = '%s', cert_type = '%s', cert_no = '%s', category = '%s', type = '%s', address = '%s', geo = '%s', ";
		$fmt .= "url = '%s', share = '%s', jj = '%s', content = '%s', pay = '%s', currency = '%s', tax = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $zjm, $fullname, $cert_type, $cert_no, $category, $type, $address, $geo, $url, $share, $jj, $content, $pay, $currency, $tax, $emp, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		} else {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		}
	
		$contact = stripslashes($request['contact']);
		$this->_contact_id($id, $contact);
	
		$z = stripslashes($request['z']);
		$this->_delete($z);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'contact/supplier/my/upload');
		return true;
	}
}
?>